﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string EmpType_Add(EmpType model ,int ACID);


[OperationContract]
string EmpType_Update(EmpType model ,int ACID);


[OperationContract]
string EmpTypeByEmpTypeID_Delete(int EmpTypeID,int ACID);


[OperationContract]
EmpType EmpTypeByEmpTypeID_Get(int EmpTypeID,int ACID );


[OperationContract]
List<EmpType> EmpType_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加EmpType表数据
/// <summary>
/// 添加EmpType
/// </summary>
/// <param name="model">EmpType实体</param>
/// <returns>空或错误</returns>
public string EmpType_Add(EmpType model,int ACID)
{
if(model.EmpTypeID==null )
{
return "『员工类别ID』不能为空！";
}
if(string.IsNullOrEmpty(model.EmptypeName))
{
return "『员工类别名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.EmpType.Count(p=>p.EmpTypeID==model.EmpTypeID)>0)
{
return "主键已存在！";
}
if(ctx.EmpType.Count(p=>p.EmptypeName==model.EmptypeName)>0)
{
return "员工类别名称已存在！";
}
ctx.EmpType.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加EmpType表数据事物用重构
/// <summary>
/// 添加EmpType事物用重构
/// </summary>
/// <param name="model">EmpType实体</param>
/// <returns>空或错误</returns>
private string EmpType_Add(EmpType model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.EmpTypeID==null )
{
return "『员工类别ID』不能为空！";
}
if(string.IsNullOrEmpty(model.EmptypeName))
{
return "『员工类别名称』不能为空！";
}
if(ctx.EmpType.Count(p=>p.EmpTypeID==model.EmpTypeID)>0)
{
return "主键已存在！";
}
if(ctx.EmpType.Count(p=>p.EmptypeName==model.EmptypeName)>0)
{
return "员工类别名称已存在！";
}
ctx.EmpType.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改EmpType表数据
/// <summary>
/// 修改EmpType
/// </summary>
/// <param name="model">EmpType实体</param>
/// <returns>空或错误</returns>
public string EmpType_Update(EmpType model,int ACID)
{
if(model.EmpTypeID==null )
{
return "『员工类别ID』不能为空！";
}
if(string.IsNullOrEmpty(model.EmptypeName))
{
return "『员工类别名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.EmpType.Count(p=>p.EmpTypeID==model.EmpTypeID)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
EmpType EmpTypemodel=ctx.EmpType.Single(p => p.EmpTypeID==model.EmpTypeID);
if(model.EmpTypeID!=null )
{
EmpTypemodel.EmpTypeID=model.EmpTypeID;
}
if(!string.IsNullOrEmpty(model.EmptypeName))
{
EmpTypemodel.EmptypeName=model.EmptypeName;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除EmpType表ByEmpTypeID数据
/// <summary>
/// 删除EmpTypeByEmpTypeID
/// </summary>
/// <param name="EmpTypeID">员工类别ID</param>
/// <returns>空或错误</returns>
public string EmpTypeByEmpTypeID_Delete(int EmpTypeID,int ACID)
{
if(EmpTypeID==null )
{
return "『员工类别ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.EmpType.Count(p=>p.EmpTypeID==EmpTypeID)!=1)
{
return null;
}
EmpType model=ctx.EmpType.Single(p => p.EmpTypeID== EmpTypeID);
ctx.EmpType.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除EmpType表ByEmpTypeID数据[事物用重构]
/// <summary>
/// 删除EmpTypeByEmpTypeID[事物用重构]
/// </summary>
/// <param name="EmpTypeID">员工类别ID</param>
/// <returns>空或错误</returns>
private string EmpTypeByEmpTypeID_Delete(int EmpTypeID,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(EmpTypeID==null )
{
return "『员工类别ID』不能为空！";
}
if(ctx.EmpType.Count(p=>p.EmpTypeID==EmpTypeID)!=1)
{
return null;
}
EmpType model=ctx.EmpType.Single(p => p.EmpTypeID== EmpTypeID);
ctx.EmpType.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取EmpType表ByEmpTypeID数据
/// <summary>
/// 获取EmpTypeByEmpTypeID
/// </summary>
/// <param name="EmpTypeID">员工类别ID</param>
/// <returns>当前表</returns>
public EmpType EmpTypeByEmpTypeID_Get(int EmpTypeID,int ACID)
{
if(EmpTypeID==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.EmpType.Count(p=>p.EmpTypeID==EmpTypeID)!=1)
{
return null;
}
return ctx.EmpType.SingleOrDefault(p=>p.EmpTypeID==EmpTypeID);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取EmpType表数据
/// <summary>
/// 获取EmpType
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<EmpType></returns>
public List<EmpType> EmpType_Get(Expression<Func<EmpType, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.EmpType.ToList();
}
 else 
{
return ctx.EmpType.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.EmpType.Where(predicate).ToList();
}
 else 
{
return ctx.EmpType.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取EmpType表数据
/// <summary>
/// 获取EmpType
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<EmpType></returns>
public List<EmpType> EmpType_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return EmpType_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取EmpType表数据计数
/// <summary>
/// 获取EmpType表数据计数
/// </summary>
/// <returns>List<EmpType></returns>
public int EmpType_GetCount(Expression<Func<EmpType, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.EmpType.Count();

}
 else
{
return ctx.EmpType.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取EmpType分页表数据
/// <summary>
/// 获取EmpType分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<EmpType></returns>
public List<EmpType> EmpType_Getpage(int pageIndex, int pageSize,Expression<Func<EmpType, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.EmpType.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.EmpType.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.EmpType.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.EmpType.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改EmpType表数据
/// <summary>
/// 批量修改EmpType
/// </summary>
/// <param name="predicate">Expression<Func<EmpType, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<EmpType,EmpType>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int EmpType_batchUpdate(Expression<Func<EmpType, bool>> predicate,Expression<Func<EmpType, EmpType>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.EmpType.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改EmpType表数据事物用重构
/// <summary>
/// 批量修改EmpType事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<EmpType, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<EmpType,EmpType>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int EmpType_batchUpdate(Expression<Func<EmpType, bool>> predicate,Expression<Func<EmpType, EmpType>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.EmpType.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除EmpType表数据
/// <summary>
/// 批量删除EmpType
/// </summary>
/// <param name="predicate">Expression<Func<EmpType, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int EmpType_batchDel(Expression<Func<EmpType, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.EmpType.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除EmpType表数据事物用重构
/// <summary>
/// 批量删除EmpType事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<EmpType, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int EmpType_batchDel(Expression<Func<EmpType, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.EmpType.Delete(predicate);
return ok;
}
#endregion


}
}

